package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlTeamMember;
import com.ruoyi.sl.service.ISlTeamMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组成员Controller
 * 
 * @author admin
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/sl/member")
public class SlTeamMemberController extends BaseController
{
    @Autowired
    private ISlTeamMemberService slTeamMemberService;

    /**
     * 查询班组成员列表
     */
    @PreAuthorize("@ss.hasPermi('sl:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlTeamMember slTeamMember)
    {
        startPage();
        List<SlTeamMember> list = slTeamMemberService.selectSlTeamMemberList(slTeamMember);
        return getDataTable(list);
    }

    /**
     * 导出班组成员列表
     */
    @PreAuthorize("@ss.hasPermi('sl:team:export')")
    @Log(title = "班组成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlTeamMember slTeamMember)
    {
        List<SlTeamMember> list = slTeamMemberService.selectSlTeamMemberList(slTeamMember);
        ExcelUtil<SlTeamMember> util = new ExcelUtil<SlTeamMember>(SlTeamMember.class);
        util.exportExcel(response, list, "班组成员数据");
    }

    /**
     * 获取班组成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:team:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return success(slTeamMemberService.selectSlTeamMemberByMemberId(memberId));
    }

    /**
     * 新增班组成员
     */
    @PreAuthorize("@ss.hasPermi('sl:team:add')")
    @Log(title = "班组成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlTeamMember slTeamMember)
    {
        if(!slTeamMemberService.checkUserUnique(slTeamMember)){
            return AjaxResult.error("用户"+slTeamMember.getNickName()+"已分配过班组！");
        }
        slTeamMember.setCreateBy(getUsername());
        return toAjax(slTeamMemberService.insertSlTeamMember(slTeamMember));
    }

    /**
     * 修改班组成员
     */
    @PreAuthorize("@ss.hasPermi('sl:team:edit')")
    @Log(title = "班组成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlTeamMember slTeamMember)
    {
        slTeamMember.setUpdateBy(getUsername());
        return toAjax(slTeamMemberService.updateSlTeamMember(slTeamMember));
    }

    /**
     * 删除班组成员
     */
    @PreAuthorize("@ss.hasPermi('sl:team:remove')")
    @Log(title = "班组成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(slTeamMemberService.deleteSlTeamMemberByMemberIds(memberIds));
    }
}
